Nil string with [NSString stringWithFormat:] appears as "(null)"

Posted by Supernico on Stack Overflow See other posts from Stack Overflow or by Supernico
Published on 2010-03-12T15:04:18Z Indexed on 2010/03/12 15:07 UTC
Read the original article Hit count: 234

Filed under:
|
|

I have a 'Contact' class with two properties : firstName and lastName. When I want to display a contact's full name, here is what I do:

NSString *fullName = [NSString stringWithFormat:@"%@ %@", contact.firstName, contact.lastName];

But when the firstName and/or lastName is set to nil, I get a "(null)" in the fullName string. To prevent it, here's what I do:

NSString *first = contact.firstName;
if(first == nil)  first = @"";
NSString *last = contact.lastName;
if(last == nil)  last = @"";
NSString *fullName = [NSString stringWithFormat:@"%@ %@", first, last];

Does someone know a better/more concise way to do this?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about string